package ig.ejb.metrica.da.grupo;


import java.sql.SQLException;
import java.lang.Exception;
import ig.da.abs.AbstractBasicDao;
import ig.ejb.metrica.da.grupo.RelaciongrupoodatoDTO;

/**
 * GenJ 2011
 * @author usuario
 *
 * Is an Class for table DB for DAO
 */
public class RelaciongrupoodatoDAO extends AbstractBasicDao{

	// -----------------------------------------------------------------
	// Constants
	// -----------------------------------------------------------------

	// -----------------------------------------------------------------
	// Attributes
	// -----------------------------------------------------------------
    private java.sql.Connection connection = null;

	// -----------------------------------------------------------------
	// Constructors
	// -----------------------------------------------------------------
    public RelaciongrupoodatoDAO ()  {

    }

	// -----------------------------------------------------------------
	// All Methods
	// -----------------------------------------------------------------

            

    /**
     * Method for create an record of table of table
     * @return : the pk of table
     * @param dto
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public  RelaciongrupoodatoDTO  create(RelaciongrupoodatoDTO dto) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;

        try{
            String QUERY = "INSERT INTO RELACIONGRUPOODATO ( IDORIGENDATO, IDGRUPOORIGENDATO ) VALUES ( ?,  ? )";

            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdorigendato());
            prep.setInt (2,dto.getIdgrupoorigendato());
            prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return dto;
    }


    /**
     * Update by Pk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int update (RelaciongrupoodatoDTO dto) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{
            String QUERY = "UPDATE RELACIONGRUPOODATO SET IDORIGENDATO = ?, IDGRUPOORIGENDATO = ? WHERE IDORIGENDATO = ?, IDGRUPOORIGENDATO = ?";
            prep = connection.prepareStatement(QUERY);
            prep.setInt (1,dto.getIdorigendato());
            prep.setInt (2,dto.getIdgrupoorigendato());

            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete by Pk
     * @param pk
     * @return the number record delete
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int delete (RelaciongrupoodatoDTO dto ) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM RELACIONGRUPOODATO WHERE IDORIGENDATO = ?, IDGRUPOORIGENDATO = ?";
            prep = connection.prepareStatement( QUERY );
            
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Find by Pk
     * @param pk
     * @return DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public RelaciongrupoodatoDTO findByPk (RelaciongrupoodatoDTO dto ) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        RelaciongrupoodatoDTO relaciongrupoodato = null;
        try{
            String QUERY = "SELECT IDORIGENDATO, IDGRUPOORIGENDATO FROM RELACIONGRUPOODATO WHERE IDORIGENDATO = ?, IDGRUPOORIGENDATO = ?";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();
            if (res.next()) {
                relaciongrupoodato = new RelaciongrupoodatoDTO(  res.getInt ( 1 ), res.getInt ( 2 ));
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,res,connection);
        }
        return relaciongrupoodato;
    }

    /**
     * Find by All
     * @return the list DTO
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public java.util.List<RelaciongrupoodatoDTO> findAll() throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<RelaciongrupoodatoDTO> list=new java.util.ArrayList<RelaciongrupoodatoDTO>();

        try{
            String QUERY = "SELECT IDORIGENDATO, IDGRUPOORIGENDATO FROM RELACIONGRUPOODATO ";
            prep = connection.prepareStatement( QUERY );
            res = prep.executeQuery();

            while (res.next()) {
                list.add(new RelaciongrupoodatoDTO(  res.getInt ( 1 ), res.getInt ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }


    public java.util.List< RelaciongrupoodatoDTO > createAll(java.util.List<RelaciongrupoodatoDTO> dtos) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        java.sql.Statement stat = null;
        java.sql.ResultSet res = null;
        java.util.List< RelaciongrupoodatoDTO > list=new java.util.ArrayList< RelaciongrupoodatoDTO >();
        try{
            String QUERY = "INSERT INTO RELACIONGRUPOODATO ( IDORIGENDATO, IDGRUPOORIGENDATO ) VALUES ( ?,  ? )";
            for(RelaciongrupoodatoDTO dto : dtos ){

                prep = connection.prepareStatement(QUERY);
                prep.setInt (1,dto.getIdorigendato());
                prep.setInt (2,dto.getIdgrupoorigendato());
                prep.executeUpdate();
                list.add(dto);
            }
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep, connection, stat, res);
        }
        return list;
    }

    /**
     * The List By idorigendato
     * @param idorigendato
     * @return list of RelaciongrupoodatoDTO
     */
    public java.util.List<RelaciongrupoodatoDTO> findByIdorigendato (Integer  idorigendato) throws SQLException, Exception {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<RelaciongrupoodatoDTO> list=new java.util.ArrayList<RelaciongrupoodatoDTO>();

        try{
            String QUERY = "SELECT IDORIGENDATO, IDGRUPOORIGENDATO FROM RELACIONGRUPOODATO WHERE IDORIGENDATO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt(1,idorigendato);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new RelaciongrupoodatoDTO(  res.getInt ( 1 ), res.getInt ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }


    /**
     * The List By idorigendato
     * @param idorigendato
     * @return list of RelaciongrupoodatoDTO
     */
    public java.util.List<RelaciongrupoodatoDTO> findByIdgrupoodato (Integer  idgrupoodatos) throws SQLException, Exception {

        java.sql.PreparedStatement prep = null;
        java.sql.ResultSet res = null;
        connection = open();
        java.util.List<RelaciongrupoodatoDTO> list=new java.util.ArrayList<RelaciongrupoodatoDTO>();

        try{
            String QUERY = "SELECT IDORIGENDATO, IDGRUPOORIGENDATO FROM RELACIONGRUPOODATO WHERE IDGRUPOORIGENDATO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt(1,idgrupoodatos);
            res = prep.executeQuery();
            while (res.next()) {
                list.add(new RelaciongrupoodatoDTO(  res.getInt ( 1 ), res.getInt ( 2 )));
            }
            return list;
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }
    


    /**
     * Delete By idorigendato
     * @param idorigendato
     * @return number Record delete
     */
    public int deleteByIdorigendato (Integer  idorigendato) throws SQLException, Exception {

        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM RELACIONGRUPOODATO WHERE IDORIGENDATO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt(1,idorigendato);
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Delete By idorigendato
     * @param idorigendato
     * @return number Record delete
     */
    public int deleteByIdGrupo (Integer  idGrupo) throws SQLException, Exception {

        java.sql.PreparedStatement prep = null;
        connection = open();

        try{
            String QUERY = "DELETE FROM RELACIONGRUPOODATO WHERE IDGRUPOORIGENDATO = ?";
            prep = connection.prepareStatement( QUERY );
            prep.setInt(1,idGrupo);
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }
    

    /**
     * Update by Fk
     * @param dto
     * @return teh number record updates
     * @throws SQLException
     * @throws Exception
     * @throws Throwable
     */
    public int updateIdorigendato (RelaciongrupoodatoDTO dto) throws SQLException, Exception {
        java.sql.PreparedStatement prep = null;
        connection = open();
        try{

            String QUERY = "UPDATE RELACIONGRUPOODATO SET IDORIGENDATO = ?, IDGRUPOORIGENDATO = ?, IDORIGENDATO = ?, IDGRUPOORIGENDATO = ? WHERE IDORIGENDATO = ?";
            prep.setInt(1,dto.getIdorigendato());
            prep.setInt(2,dto.getIdgrupoorigendato());


            prep = connection.prepareStatement(QUERY);
            return prep.executeUpdate();
        } catch (SQLException ex) {
            throw ex;
        } catch (Exception ex) {
            throw ex;
        } finally {
            close(prep,connection);
        }
    }

    /**
     * Open the connection
     * @return
     * @throws java.lang.ClassNotFoundException
     * @throws java.sql.SQLException
     */
    private java.sql.Connection open() throws ClassNotFoundException, java.sql.SQLException,Exception {
    	return getConnection("METRICADW");
    }



}
